package com.zyw.dev.zyw.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.zyw.dev.zyw.R;
import com.zyw.dev.zyw.db.DataBaseHealper;
import com.zyw.dev.zyw.utils.SPUtils;

import java.util.Date;

public class WelcomeActivity extends AppCompatActivity {


    private final long SPLASH_LENGTH = 2000;
    Handler handler = new Handler();
    private String loginCacheKey;

    private long lastLogintTime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        DataBaseHealper.newInstance().initDataBase();

        loginCacheKey = (String) SPUtils.get(this,LoginActivity.LOGIN_CACHE_KEY,"");
        if (null != loginCacheKey && loginCacheKey.length() > 0){
            lastLogintTime = new Date().getTime()-Long.parseLong(loginCacheKey.substring(loginCacheKey.indexOf("@")+1));
        }
        handler.postDelayed(new Runnable() {  //使用handler的postDelayed实现延时跳转

            public void run() {
                Intent intent = null;
                if (null == loginCacheKey || "" == loginCacheKey || lastLogintTime>5*60*1000){
                    intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                }else{
                    intent = new Intent(WelcomeActivity.this, MainActivity.class);
                    intent.putExtra("isFromLogin",true);
                    intent.putExtra("name",loginCacheKey.substring(0,loginCacheKey.indexOf("@")));
                }
                startActivity(intent);
                WelcomeActivity.this.finish();
            }
        }, SPLASH_LENGTH);//2秒后跳转至应用主界面MainActivity
    }
}
